package com.gmall.cart.controller;

import com.gmall.cart.pojo.Cart;
import com.gmall.cart.service.CartService;
import com.gmall.common.bean.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping
    public String saveCart(Cart cart){
        this.cartService.saveCart(cart);
        return "redirect:http://cart.gmall.com/addCart.html?skuId=" + cart.getSkuId() + "&count=" + cart.getCount();
    }

    @GetMapping("addCart.html")
    public String addCart(Model model, Cart cart){
        // 获取本次新增购物车的数量
        BigDecimal count = cart.getCount();
        // 查询购物车中对应的记录
        cart = this.cartService.queryCartBySkuId(cart.getSkuId());
        // 使用本次新增的数量覆盖掉数据库中的数量
        cart.setCount(count);
        model.addAttribute("cart", cart);
        return "addCart";
    }

    @GetMapping("cart.html")
    public String queryCarts(Model model){
        List<Cart> carts = this.cartService.queryCarts();
        model.addAttribute("carts", carts);
        return "cart";
    }

    @PostMapping("updateNum")
    @ResponseBody
    public ResponseVo updateNum(@RequestBody Cart cart){
        this.cartService.updateNum(cart);
        return ResponseVo.ok();
    }

    @PostMapping("deleteCart")
    @ResponseBody
    public ResponseVo deleteCart(@RequestParam("skuId")Long skuId){
        this.cartService.deleteCart(skuId);
        return ResponseVo.ok();
    }

    @GetMapping("test")
    @ResponseBody
    public String test(HttpServletRequest request){
        //System.out.println("controller方法  ===  handler" + LoginInterceptor.getUserInfo());
        System.out.println("===============controller方法开始执行==============");
        long now = System.currentTimeMillis();
        this.cartService.execute1();
        this.cartService.execute2();
//        ListenableFuture<String> future1 = this.cartService.execute1();
//        ListenableFuture<String> future2 = this.cartService.execute2();
//        future1.addCallback(result -> {
//            System.out.println("execute1执行成功：" + result);
//        }, ex -> {
//            System.out.println("execute1执行失败：" + ex.getMessage());
//        });
//        future2.addCallback(result -> {
//            System.out.println("execute2执行成功：" + result);
//        }, ex -> {
//            System.out.println("execute2执行失败：" + ex.getMessage());
//        });
//        try {
//            System.out.println(future1.get() + "======" + future2.get());
//        } catch (Exception e) {
//            System.out.println("====" + e.getMessage());
//        }
        System.out.println("===========controller方法执行结束=========" + (System.currentTimeMillis() - now));
        return "hello cart!";
    }
}
